package jetbrains.buildServer.cuta.logic.operators;

import jetbrains.buildServer.cuta.logic.ColumnDef;
import org.jdom.Element;

import java.util.Map;
import java.util.TreeMap;



/**
 * @author Leonid Bushuev from JetBrains
 */

public final class AllOperators
{

    /**
     * Registry of all possible operators.
     * Maps operator tag to operator class.
     */
    public static final Map<String, Class<? extends AbstractOperator>> operators =
            new TreeMap<String, Class<? extends AbstractOperator>>();


    static
    {
        operators.put("sequence", SequenceOperator.class);
        operators.put("get-property", GetPropertyOperator.class);
        operators.put("get-env-var", GetEnvVarOperator.class);
        operators.put("get-system-var", GetSystemVarOperator.class);
        operators.put("get-config-var", GetConfigVarOperator.class);
        operators.put("get-agent-policy", GetAgentPolicyOperator.class);
        operators.put("replace", ReplaceOperator.class);
        operators.put("join-lines", JoinLinesOperator.class);
    }


    public static AbstractOperator makeOperator(Element element, ColumnDef column, ExternalContext extContext)
    {
        String tag = element.getName().toLowerCase();
        try
        {
            Class<? extends AbstractOperator> theClass = operators.get(tag);
            AbstractOperator operator = theClass.newInstance();
            operator.init(element, column, extContext);
            return operator;
        }
        catch (Exception e)
        {
            throw new RuntimeException("Could not parse/instantiate operator " + tag + ".", e);
        }
    }
}
